package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Timmy
 * Date: 8/7/13
 * Time: 10:56 AM
 * To change this template use File | Settings | File Templates.
 */
public class BookShelf {


    public static final String NO_AVAILABLE_BOOKS = "No available books\n";
    private HashMap<Integer,Book> bookList;

    public BookShelf() {
        bookList = new HashMap<Integer, Book>();
    }

    public void addBook(Book libraryBook) throws BookAlreadyExistsException {
        if(getBookWithISBN(libraryBook.getBookISBN())==null){
            bookList.put(libraryBook.getBookISBN(),libraryBook);
        }else{
            throw new BookAlreadyExistsException();
        }

    }

    public int numberOfBooks() {
        return bookList.size();  //To change body of created methods use File | Settings | File Templates.
    }

    public Book getBookWithISBN(int ISBN) {
        return bookList.get(ISBN);
    }

    public String getBookList() {
        String bookInformation="";
        if(numberOfBooks()>0){
            Iterator it = bookList.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry pairs = (Map.Entry)it.next();
                Book book = (Book) pairs.getValue();
                bookInformation+=pairs.getKey()+"\t"+book.getBookTitle()+"\t"+book.getBookAuthor()+"\n";
            }
        }else{
            bookInformation= NO_AVAILABLE_BOOKS;
        }
        return bookInformation;  //To change body of created methods use File | Settings | File Templates.
    }

    public boolean hasBookWith(int ISBN) {
        return getBookWithISBN(ISBN) != null ? true : false;
    }
}
